package com.csjbot.snowbot_rogue.servers.serials;

import android.util.SparseArray;

import com.android.core.util.JsonUtil;
import com.csjbot.csjbase.log.Csjlogger;

import java.util.LinkedHashMap;

/**
 * Created by zouyh on 2017/5/27.
 */

public class SelfCheckParser {


    //  00      左手臂电机
    //  01      右手臂电机
    //  02      头部触摸传感器

    public static SparseArray<String> upBodyArray = new SparseArray<>();

    static {
        upBodyArray.put(0, "左手臂电机");
        upBodyArray.put(1, "右手臂电机");
        upBodyArray.put(2, "头部触摸传感器");
    }


    //    00	下视左传感器
//01 	下视中传感器
//02	下视右传感器
//03	后视左传感器
//04	后视右传感器
//05	超声波传感器
//06
//        07
//        08	左轮电机
//09	右轮电机
//10	急停信号
//11
//        12
//        13
//        14
//        15
    public static SparseArray<String> downBodyArray = new SparseArray<>();

    static {
        downBodyArray.put(0, "下视左传感器");
        downBodyArray.put(1, "下视中传感器");
        downBodyArray.put(2, "下视右传感器");
        downBodyArray.put(3, "后视左传感器");
        downBodyArray.put(4, "后视右传感器");
        downBodyArray.put(5, "超声波传感器");
        downBodyArray.put(6, "下视右传感器");
        downBodyArray.put(7, "保留");
        downBodyArray.put(8, "左轮电机");
        downBodyArray.put(9, "右轮电机");
        downBodyArray.put(10, "急停信号");
    }

    public static LinkedHashMap<String, String> letHumanReadAbleUpBodyCheck(int status) {
        LinkedHashMap<String, String> upCheck = new LinkedHashMap<>();
        for (int i = 0; i < 32; i++) {
            int mask = (1 << i);
            Csjlogger.debug("第{}位 是 {}", i, (status & mask) == 0 ? 0 : 1);

            if (upBodyArray.get(i) != null) {
                if ((status & mask) == 0) {
                    upCheck.put(upBodyArray.get(i), "OK");
                } else {
                    upCheck.put(upBodyArray.get(i), "NG");
                }
            }
        }

        return upCheck;
    }

    public static LinkedHashMap<String, String> letHumanReadAbleDownBodyCheck(int status) {
        LinkedHashMap<String, String> upCheck = new LinkedHashMap<>();
        for (int i = 0; i < 32; i++) {
            int mask = (1 << i);
            Csjlogger.debug("第{}位 是 {}", i, (status & mask) == 0 ? 0 : 1);

            if (downBodyArray.get(i) != null) {
                if ((status & mask) == 0) {
                    upCheck.put(downBodyArray.get(i), "OK");
                } else {
                    upCheck.put(downBodyArray.get(i), "NG");
                }
            }
        }

        return upCheck;
    }

    public static String checkToString(LinkedHashMap<String, String> result) {
        return JsonUtil.map2Json(result);
    }
}
